﻿using System.Configuration;
using contSys.database.DAL;
using System.Data;

using contSys.Security;
using System.Diagnostics;
using System;

namespace contSys.BL.Simulator
{
    public class Plans
    {
        private static string _connString = ConfigurationManager.ConnectionStrings["Simulator"].ConnectionString;
        private static string tableObjName = "Plans";

        public static string LastErrorMsg = string.Empty;

        public static DataSet GetPlansByUnit(string usr, string pwd, object unitID)
        {
            if (UserManager.QueryAllowed(tableObjName, usr, pwd))
            {
                LastErrorMsg = string.Empty;
                string sql = string.Format("SELECT * FROM Plans WHERE UnitID = {0}", unitID.ToString());
                return SQLiteDB.GetRecords(_connString, sql);
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return null;
        }

        private static bool checkPlanValidity(object cyc, object offset)
        {
            int c, o;

            LastErrorMsg = string.Empty;

            if (!int.TryParse(cyc.ToString(), out c) || c < 10)
            {
                LastErrorMsg = "Cycle has to be a number>=10";
                return false;
            }

            if (!int.TryParse(offset.ToString(), out o) || o <= 0 || o >= c)
            {
                LastErrorMsg = "Offset has to be: 0<Offset<Cycle";
                return false;
            }

            return true;
        }

        public static bool ModifyPlan(string usr, string pwd, object unitID, object planID, object cyc, object offset, object descr)
        {
            if (UserManager.ModifyAllowed(tableObjName, usr, pwd))
            {
                if (checkPlanValidity(cyc, offset))
                {
                    LastErrorMsg = string.Empty;
                    string sql = string.Format("UPDATE Plans SET CYC={0}, Offset={1}, Description='{2}' WHERE (UnitID={3} AND PlanID={4})", cyc.ToString(), offset.ToString(), descr.ToString(), unitID.ToString(), planID.ToString());
                    return SQLiteDB.TryExecuteScalar(_connString, sql);
                }
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return false;
        }

        public static bool AddPlan(string usr, string pwd, object unitID, object planID, object cyc, object offset, object descr)
        {
            if (UserManager.AddAllowed(tableObjName, usr, pwd))
            {
                if (checkPlanValidity(cyc, offset))
                {
                    LastErrorMsg = string.Empty;
                    string sql = string.Format("INSERT INTO Plans (UnitID, planID, cyc, offset, Description) VALUES ({0},{1},{2},{3},'{4}')", unitID.ToString(), planID.ToString(), cyc.ToString(), offset.ToString(), descr.ToString());
                    return SQLiteDB.TryExecuteScalar(_connString, sql);
                }
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return false;
        }

        public static bool DeletePlan(string usr, string pwd, object unitID, object planID)
        {
            if (UserManager.DeleteAllowed(tableObjName, usr, pwd))
            {
                LastErrorMsg = string.Empty;
                return SQLiteDB.TryExecuteScalar(_connString, string.Format("DELETE FROM Plans WHERE (UnitID={0} AND PhaseID={1})", unitID.ToString(), planID.ToString()));
            }
            return false;
        }

        private static void Writelog(string msg)
        {
            const string cs = "contSys.BL.Simulator";

            EventLog elog = new EventLog();
            if (!EventLog.SourceExists(cs))
                EventLog.CreateEventSource(cs, cs);

            elog.Source = cs;
            elog.EnableRaisingEvents = true;
            elog.WriteEntry(msg);
        }

    }
}
